﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.Utility.Common;
using System.Data;
using System.Web.Services;
using Basic4Net.OAWeb.Base;
using System.Text;

namespace Basic4Net.OAWeb.Module_Attendance
{
    public partial class Attendance : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                classbind();
                typebind();
                //SetTypeBind();
            }
        }

        private void classbind()
        {
            //this.ddl_FType.DataValueField = "RID";
            //this.ddl_FType.DataTextField = "PostName";
            //this.ddl_FType.DataSource = (new BusinessRules.Post()).FindAll(SchoolMark);
            //this.ddl_FType.DataBind();
            //ListItem item1 = new ListItem();
            //item1.Value = "0";
            //item1.Text = "尚未分类";
            //this.ddl_FType.Items.Add(item1);
            //ListItem item2 = new ListItem();
            //item2.Value = "-1";
            //item2.Text = "----全部----";
            //this.ddl_FType.Items.Add(item2);
            //this.ddl_FType.SelectedValue = "-1";

            this.ddl_FType.DataValueField = "RID";
            this.ddl_FType.DataTextField = "TypeName";
            this.ddl_FType.DataSource = (new BusinessRules.TeacherType()).ListAll();
            ddl_FType.DataBind();
        }

        private void typebind()
        {
            DataTable dt = new DataTable();
            Common.IListToTable(dt, new BusinessRules.ListTeacher());
            IList<BusinessRules.ListTeacher> mylist = (new BusinessRules.Attendance()).ListTeacher(int.Parse(this.ddl_FType.SelectedValue), this.txt_TeacherCode.Text, this.txt_TeacherName.Text);
            for (int i = 0; i < mylist.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["TeacherID"] = mylist[i].TeacherID;
                row["TeachCode"] = mylist[i].TeachCode;
                row["TeachName"] = mylist[i].TeachName;
                row["TypeName"] = mylist[i].TypeName;
                row["Sexy"] = mylist[i].Sexy;
                row["Birthday"] = mylist[i].Birthday;
                dt.Rows.Add(row);
            }

            //------------------------------------------------------------------------分页处理开始......
            int PageNow = 0;
            DataView dv = dt.DefaultView;
            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dv;
            ps.AllowPaging = true;
            ps.PageSize = 20;
            if (this.txt_Go.Text.ToString() == "")
                this.txt_Go.Text = "1";
            if (this.txt_Go.Text.ToString() == "0")
                this.txt_Go.Text = "1";
            if (Convert.ToInt32(this.txt_Go.Text.ToString()) >= ps.PageCount)
                this.txt_Go.Text = ps.PageCount.ToString();
            PageNow = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //this.lbn_Up.Enabled = true;
            //this.lbn_Next.Enabled = true;
            //if (PageNow == 0)
            //{
            //    this.lbn_Up.Enabled = false;
            //}
            //if (PageNow == ps.PageCount - 1)
            //{
            //    this.lbn_Next.Enabled = false;
            //}
            ps.CurrentPageIndex = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //-------------------------------------------------------------------------分页处理结束

            this.rpt_TeacherType.DataSource = ps;
            this.rpt_TeacherType.DataBind();

            //------------------------------------------------------------------------记录总数、当前页数、总页数处理开始......
            //this.lbl_SumCount.Text = ds.Tables[0].Rows.Count.ToString();
            this.lbl_ToPage.Text = this.txt_Go.Text.ToString();
            this.lbl_PageCount.Text = ps.PageCount.ToString();

            //------------------------------------------------------------------------------记录总数、当前页数、总页数处理结束
        }

        protected void btnBind_Click(object sender, EventArgs e)
        {
            typebind();
        }

        protected void btnBind2_Click(object sender, EventArgs e)
        {
            string msg = string.Empty;
            Entities.t_Attendance model = new Entities.t_Attendance();
            model.FYear = GetProDateYear();
            model.FMonth = GetProDateMonth();
            model.TeacherID = int.Parse(this.hdf_TeacherID.Value);
            model.Late = int.Parse(this.txt_Late.Text);
            model.Early = int.Parse(this.txt_Early.Text);
            model.Truant = int.Parse(this.txt_Truant.Text);
            model.Absenteeism = int.Parse(this.txt_Absenteeism.Text);
            model.Sick = int.Parse(this.txt_Sick.Text);
            model.Leave = int.Parse(this.txt_Leave.Text);
            model.Other = Convert.ToDouble(this.txt_Other.Text);
            model.OtherContect = this.txt_OtherContect.Text;
            model.SchoolMark = SchoolMark;
            if (model.Late == 0 && model.Early == 0 && model.Truant == 0 && model.Absenteeism == 0 && model.Sick == 0 && model.Leave == 0 && model.Other==0)
            {
                (new BusinessRules.Attendance()).Del(GetProDateYear(), GetProDateMonth(), int.Parse(this.hdf_TeacherID.Value), ref msg);
            }
            else
            {
                (new BusinessRules.Attendance()).Add(model, ref msg);
            }
        }

        [System.Web.Services.WebMethod]
        public static string[] LoadAtt(int TeacherID)
        {
            StringBuilder str = new StringBuilder();
            Entities.t_Attendance model = (new BusinessRules.Attendance()).GetModel(GetProDateYear(),GetProDateMonth(),TeacherID);
            if (model != null)
            {
                str.Append(model.Late);
                str.Append("|");
                str.Append(model.Early);
                str.Append("|");
                str.Append(model.Truant);
                str.Append("|");
                str.Append(model.Absenteeism);
                str.Append("|");
                str.Append(model.Sick);
                str.Append("|");
                str.Append(model.Leave);
                str.Append("|");
                str.Append(model.Other);
                str.Append("|");
                str.Append(model.OtherContect);
                str.Append("|");
            }
            else
            {
                str.Append(0);
                str.Append("|");
                str.Append(0);
                str.Append("|");
                str.Append(0);
                str.Append("|");
                str.Append(0);
                str.Append("|");
                str.Append(0);
                str.Append("|");
                str.Append(0);
                str.Append("|");
                str.Append(0.0);
                str.Append("|");
                str.Append("");
                str.Append("|");
            }
            return str.ToString().Split('|');
        }

        protected void txt_OtherContect_TextChanged(object sender, EventArgs e)
        {
            System.Text.RegularExpressions.Regex objRegExp = new System.Text.RegularExpressions.Regex("<[^>]*>");
            this.txt_OtherContect.Text = objRegExp.Replace(this.txt_OtherContect.Text, "");
        }

    }
}